-module(p30).
-compile(export_all).

main(Bound) -> main(2, Bound).

main(Bound1, Bound2) -> [X || X <- lists:seq(Bound1, Bound2),
                              X =:= sum_of_powers(X, 5)].

sum_of_powers(Num, Pow) -> sum_of_digits(Num, n_power(Pow)).

n_power(N) -> fun(X) -> erlang:trunc(math:pow(X, N)) end.

to_digit(Symbol) -> erlang:list_to_integer([Symbol]).

pow_4(Num) -> erlang:trunc(math:pow(Num, 4)).

sum_of_digits(Number, Func) ->
    SymbolsList = erlang:integer_to_list(Number),
    DigitsList = lists:map(fun to_digit/1, SymbolsList),
    AppliedList = lists:map(Func, DigitsList),
    lists:sum(AppliedList).
